package com.example.demo.config;

import com.example.demo.component.ChatRoom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
public class RegWebSocket implements WebSocketConfigurer {

    @Autowired
    private ChatRoom chatRoom;

    /**
     * 注册 webSocket，与 servlet 共享一个 session 信息
     * @param registry
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(chatRoom, "/chat")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }

}
